package com.mini.web.controller.front;

import com.mini.common.core.controller.BaseController;
import com.mini.common.core.domain.AjaxResult;
import com.mini.common.core.page.TableDataInfo;
import com.mini.common.utils.SecurityUtils;
import com.mini.system.domain.question.PracticeRecordDTO;
import com.mini.system.domain.question.Question;
import com.mini.system.service.IPracticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author mini wjw支持
 * 2025.06.30
 */

@Api(tags = "练习相关接口")
@RestController
@RequestMapping("/front/practice")
@RequiredArgsConstructor
public class PracticeController extends BaseController {

    private final IPracticeService practiceService;

    @ApiOperation("生成练习题目")
    @PostMapping("/generate")
    public AjaxResult generateQuestions(@RequestBody Map<String, Object> params) {
        String difficulty = (String) params.get("difficulty");
        Integer questionCount = (Integer) params.get("questionCount");
        String type = (String) params.get("type");

        List<Question> questions = practiceService.generateQuestions(difficulty, questionCount, type);
        return success(questions);
    }

    @ApiOperation("提交答案")
    @PostMapping("/submit")
    public AjaxResult submitAnswers(@RequestBody PracticeRecordDTO record) {
        Map<String, Object> result = practiceService.submitAndAnalyze(record);
        return success(result);
    }

    @ApiOperation("获取练习历史")
    @GetMapping("/history")
    public TableDataInfo getPracticeHistory(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        startPage();
        return getDataTable(practiceService.getPracticeHistory());
    }


    @ApiOperation("获取AI分析")
    @PostMapping("/ai-analysis")
    public AjaxResult getAIAnalysis(@RequestBody PracticeRecordDTO record) {
        String analysis = practiceService.getAIAnalysis(record);
        return success(analysis);
    }

    /**
     * 获取用户练习统计数据
     */
    @GetMapping("/stats")
    @ApiOperation("获取用户练习统计数据")
    public AjaxResult getStats() {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        return AjaxResult.success(practiceService.getUserStats(userId));
    }
}
